package cn.wolfcde.luowowo.website.util;

import cn.wolfcode.luowowo.common.customexception.UserInfoException;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class CommonExceptionHandler {

    //针对自定义异常的处理
    @ExceptionHandler(UserInfoException.class)
    public void UserInfoExceptionHandler(Exception e, HttpServletResponse response) throws IOException {

        response.setContentType("text/json;charset=utf-8");

        AjaxResult ajaxResult = new AjaxResult();

        ajaxResult.mark(e.getMessage());

        response.getWriter().write(JSON.toJSONString(ajaxResult));

    }

//    //针对系统异常的处理
//    @ExceptionHandler(Exception.class)
//    public void ExceptionHandler(Exception e, HttpServletResponse response) throws IOException {
//
//        response.setContentType("text/json;charset=utf-8");
//
//        AjaxResult ajaxResult = new AjaxResult();
//
//        ajaxResult.mark("系统繁忙,请稍候重试");
//
//        response.getWriter().write(JSON.toJSONString(ajaxResult));
//
//    }
}
